package cn.org.gs.channel;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo2 {
    public static void main(String[] args) throws Exception {
        // 打开FileChannel
        RandomAccessFile file = new RandomAccessFile("d:\\01.txt", "rw");
        FileChannel channel = file.getChannel();
        // 创建Buffer对象
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        String newData = "data atguigu";
        buffer.clear();

        // 写入内容
        buffer.put(newData.getBytes());

        buffer.flip();

        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }

        channel.close();

    }
}
